package com.mycompany.app.hr.common.pagination.displaytag;

import javax.servlet.http.HttpServletRequest;

/**
 * 
 * @author Shawn.guo
 */
public class DisplayTagPaginationHelper {

    public static Integer getCurrentPageNum(HttpServletRequest request,
            String tableId) {

        Integer currentPageNum = 0;

        try {
            currentPageNum = Integer
                    .valueOf(request
                            .getParameter(new org.displaytag.util.ParamEncoder(
                                    tableId)
                                    .encodeParameterName(org.displaytag.tags.TableTagParameters.PARAMETER_PAGE)));
        } catch (NumberFormatException e) {
            return 0;
        }

        return currentPageNum;
    }

    /**
     * @param request
     * @param tableId
     * @return
     */
    public static Boolean isSortingAsc(HttpServletRequest request,
            String tableId) {

        // Descending is 2, Ascending is 1.
        Boolean order = Boolean.TRUE;
        String paraOrder = request
                .getParameter(new org.displaytag.util.ParamEncoder(tableId)
                        .encodeParameterName(org.displaytag.tags.TableTagParameters.PARAMETER_ORDER));
        if (paraOrder != null && Integer.valueOf(paraOrder) == 1)
            order = Boolean.TRUE;
        else
            order = Boolean.FALSE;

        return order;
    }

    public static String getSortingColumnName(HttpServletRequest request,
            String tableId) {

        return request
                .getParameter(new org.displaytag.util.ParamEncoder(tableId)
                        .encodeParameterName(org.displaytag.tags.TableTagParameters.PARAMETER_SORT));
    }

}
